package com.robot.member.controller;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.robot.member.client.MerchantClient;
import com.robot.member.pojo.Cart;
import com.robot.member.pojo.Member;
import com.robot.member.pojo.client.Store;
import com.robot.member.service.AddressService;
import com.robot.member.service.CartService;
import com.robot.member.service.MemberService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.robot.member.pojo.CartDetails;
import com.robot.member.service.CartDetailsService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import util.CheckUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * details控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/details")
public class CartDetailsController {
    @Autowired
    private CartDetailsService cartDetailsService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.SUCCESS_QUERY, cartDetailsService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.SUCCESS_QUERY, cartDetailsService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<CartDetails> pageList = cartDetailsService.findSearch(searchMap, page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<CartDetails>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.SUCCESS_QUERY, cartDetailsService.findSearch(searchMap));
    }


    /**
     * 修改
     *
     * @param details
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody CartDetails details, @PathVariable String id) {
        details.setId(id);
        cartDetailsService.update(details);
        return new Result(StatusCode.SUCCESS_UPDATED);
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        cartDetailsService.deleteById(id);
        return new Result(StatusCode.SUCCESS_DELETED);
    }

}
